<?php

/**
 * 导航条标签
 * <a href="/">首页</a>-<a href="/channels/108.html">第一级栏目</a>-
 * <a href="/channels/127.html">第二级栏目</a>-<a href="/location/">最后一级栏目</a>
 * @author tony <tony@yonglang.co>
 * @version 1.0
 */
class YlLocationTaglib {

    /**
     * 解析模板中的 <yl:location firstClass="first" lastClass="last" separator=">" target="_blank"></yl:location>标签
     *      属性： 1.firstClass 第一个a标签的样式,默认为 first
     *            2.lastlass 最后一个a标签的样式,默认为 last
     *            3.separator a标签之间的分隔符,默认为 >
     *            4.target a标签的target属性，默认为 _blank
     * @param string $content 需要解析的内容
     * @param array $cates 栏目信息
     * @param array $globalConfig 全局配置，优先级最低
     * @param array $sites 站点信息
     * @param int $contentId 内容ID
     * @return string 解析后的内容
     */
    public function parse($content, $cates, $sites, $globalConfig, $contentId = 0) {
        if (stripos($content, 'yl:location') !== false) {
            $matches = preg_parse($content, 'yl:location');
            foreach ($matches as $match) {

                /** 导航条首个a标签的样式 */
                $firstClass = getParseOneAttr($match, 'firstClass');
                /** 导航条最后一个a标签的样式 */
                $lastClass = getParseOneAttr($match, 'lastClass');
                /** 导航条每个元素（a标签）之间的分隔符 */
                $separator = getParseOneAttr($match, 'separator');
                /** a标签的打开方式 */
                $target = getParseOneAttr($match, 'target');

                $target = empty($target) ? '_blank' : $target;
                $separator = empty($separator) ? '&gt;' : $separator;
                $firstClass = empty($firstClass) ? 'first' : $firstClass;
                $lastClass = empty($lastClass) ? 'last' : $lastClass;

                $navBar = '';
                $pIds = explode(',', $cates['plistid']);
                $flag = false;
                foreach ($pIds as $pid) {
                    $channelInfo = array();
                    //取得父栏目信息
                    if (false === ($channelInfo = S('cache_channel_' . $pid))) {
                        $channelInfo = M('cms_category')
                                ->field('(case when url <> "" then url when createurl <> "" then createurl
                                        else (concat("' . $sites['domain'] . '","/",replace(catenaming,"{cateid}",id))) end) as cateurl,
                                        replace(imageurl,"@","' . $sites['domain'] . '") as imageurl,id,plistid,cname,content,keywords,
                                        description,catetemplate,arttemplate,createurl,catenaming,artnaming,pagetitle,pagedescription,
                                        inputtime,updatetime,content_num,pid')
                                ->where('siteid=' . $sites['siteid'] . ' and id=' . $pid)
                                ->find();
                        S('cache_channel_' . $pid, $channelInfo);
                    }

                    $channelUrl = $this->_getChannelUrl($channelInfo, $sites['domain'], $globalConfig);
                    //拼接导航条
                    if ($flag == true) {
                        $navBar .= '<a href="' . $channelUrl . '" target="' . $target . '">' . $channelInfo['cname'] . '</a><span>' . $separator . '</span>';
                    } else {
                        $navBar .= '<a class="' . $firstClass . '" href="' . $channelUrl . '" target="' . $target . '">' . $channelInfo['cname'] . '</a><span>' . $separator . '</span>';
                        $flag = true;
                    }
                }
                
                $channelUrl = $this->_getChannelUrl($cates, $sites['domain'], $globalConfig);
                $navBar .= '<a class="' . $lastClass . '" href="' . $channelUrl . '" target="' . $target . '">' . $cates['cname'] . '</a>';

                $content = str_replace($match, $navBar, $content);
            }
        }
        return $content;
    }

    /**
     * 获取栏目的url链接
     *      cateurl优先级顺序： url(外链) --> createurl(指定的路径) --> catenaming(当前栏目的url规则) --> globalConfig['catenaming'](全局的栏目url规则)
     * @param type $channels 栏目信息
     * @param type $globalConfig 全局配置
     * @return string 栏目链接
     */
    private function _getChannelUrl($channelInfo, $domain, $globalConfig) {
        if (!empty($channelInfo['createurl'])) {
            $channelInfo['cateurl'] = $channelInfo['createurl'];
        } else {
            //启用全局规则
            if (!empty($channelInfo['catenaming'])) {
                $channelInfo['cateurl'] = common('Array')->getParseTemplate($channelInfo, $channelInfo['catenaming'], 'cateid');
            } else {
                $channelInfo['cateurl'] = common('Array')->getParseTemplate($channelInfo, $globalConfig['catenaming'], 'cateid');
            }
            $channelInfo['cateurl'] = str_replace('{@ChannelID}', $channelInfo['id'], $channelInfo['cateurl']);
            $channelInfo['cateurl'] = str_replace('{@Year}', date("Y"), $channelInfo['cateurl']);
            $channelInfo['cateurl'] = $domain . '/' . ltrim($channelInfo['cateurl'], '/');
        }
        return $channelInfo['cateurl'];
    }

}
